package com.duoec.magichour.core.web.interceptor;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by ycoe on 17/6/25.
 */
public class MonitorInterceptor implements MethodInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(MonitorInterceptor.class);

    @Override
    public Object invoke(MethodInvocation methodProxy) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result;
        result = methodProxy.proceed();
        long endTime = System.currentTimeMillis();
        long timeCost = endTime - startTime;
        if (timeCost > 800) {
            String server = methodProxy.getThis().getClass().getSimpleName();
            String method = methodProxy.getMethod().getName();
            String classMethod = server + "." + method;
            logger.warn("慢响应：{},{}ms", classMethod, timeCost);
        }
        return result;
    }
}
